<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    :show-ok-btn="false"
  >
    <div class="flex justify-center items-center h-full">
      <img :src="path" alt="" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { getShowImg } from '/@/api/system//system';
  import { bufToUrl } from '/@/utils/file/base64Conver';

  export default defineComponent({
    components: { BasicModal },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      let path = ref<any>(null);
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        setModalProps({ confirmLoading: false, width: '30%' });
        const res = await getShowImg({ path: data?.path });
        path.value = bufToUrl(res);
      });

      const getTitle = '预览';

      async function handleSubmit() {
        try {
          closeModal();
          emit('success');
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        getTitle,
        handleSubmit,
        path,
      };
    },
  });
</script>
